﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication1.DTO;
using WindowsFormsApplication1.DAO;

namespace WindowsFormsApplication1
{
    public partial class CongViec : Form
    {
        public CongViec()
        {
            InitializeComponent();
        }

        private void CongViec_Load(object sender, EventArgs e)
        {
            LoadGiaoVien();
            LoadNamHoc();
            LoadHocKy();
            LoadLanThi();
            LoadLoaiCongViec();
        }

        private void LoadLoaiCongViec()
        {
            List<LoaiCongViecDTO> ds = LoaiCongViecDAO.GetList();
            foreach (LoaiCongViecDTO lcv in ds)
                cbCongViec.Items.Add(lcv);
            cbGiaoVien.SelectedIndex = 0;
        }

        private void LoadGiaoVien()
        {
            List<GiaoVienDTO> ds = GiaoVienDAO.GetList();
            foreach (GiaoVienDTO gv in ds)
                cbGiaoVien.Items.Add(gv);
            cbGiaoVien.SelectedIndex = 0;
        }

        private void LoadLanThi()
        {
            List<LanThiDTO> ds = LanThiDAO.GetList();
            cbLanThi.Text = ds[0].tenLanThi;
            foreach (LanThiDTO lt in ds)
                cbLanThi.Items.Add(lt);
            cbLanThi.SelectedIndex = 0;
        }

        private void LoadNamHoc()
        {
            List<NamHocDTO> ds = NamHocDAO.GetList();
            cbNamHoc.Text = ds[0].value;
            foreach (NamHocDTO nam in ds)
                cbNamHoc.Items.Add(nam);
            cbNamHoc.SelectedIndex = 0;
        }

        private void LoadHocKy()
        {
            List<HocKyDTO> ds = HocKyDAO.GetList();
            cbHocKi.Text = ds[0].value;
            foreach (HocKyDTO hk in ds)
                cbHocKi.Items.Add(hk);
            cbHocKi.SelectedIndex = 1;
        }

        private void btOK_Click(object sender, EventArgs e)
        {
            GiaoVien_CongViecDTO cvdto = new GiaoVien_CongViecDTO();
            cvdto.maGiaoVien = ((GiaoVienDTO)cbGiaoVien.SelectedItem).maGiaoVien;
            cvdto.namHoc = ((NamHocDTO)cbNamHoc.SelectedItem).id;
            cvdto.hocKy = ((HocKyDTO)cbHocKi.SelectedItem).id;
            cvdto.loaiCongViec = ((LoaiCongViecDTO)cbCongViec.SelectedItem).id;
            cvdto.lanThi = ((LanThiDTO)cbLanThi.SelectedItem).id;
            try
            {
                cvdto.soLuongThucHien = int.Parse(txtSoLuong.Text);
            }
            catch(Exception)
            {
                cvdto.soLuongThucHien = 0;
            }
            if (GiaoVien_CongViecDAO.Insert(cvdto))
                MessageBox.Show("OK");
            else
                MessageBox.Show("Đã có lỗi xảy ra. Công việc chưa được thêm.");
        }
    }
}
